/*
*
* Copyright (c) 2011 Vaulting Systems International
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy 
* of this software and associated documentation files (the "Software"), to deal 
* in the Software without restriction, including without limitation the rights 
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
* of the Software, and to permit persons to whom the Software is furnished to do  
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all  
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE  
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package com.securecreditlib.test;

import java.math.BigDecimal;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

import com.ekeyman.securecreditlib.business.CardTransactionBO;
import com.ekeyman.securecreditlib.business.CreditCardBO;
import com.ekeyman.securecreditlib.business.RegistryBO;
import com.ekeyman.securecreditlib.domain.CardTransaction;
import com.ekeyman.securecreditlib.domain.CreditCard;
import com.ekeyman.securecreditlib.domain.Registry;
import com.ekeyman.securecreditlib.service.EkeymanService;
import com.ekeyman.securecreditlib.util.SecureCreditUtils;
import com.ekeymanlib.dto.EncryptionKeys;

public class End2EndUT {
	
	private static final String[] SPRING_CONFIG_FILES = new String[]{"applicationContext-lib.xml"};
	ClassPathXmlApplicationContext ctx;
	
	private CreditCardBO creditCardBO;
	private CardTransactionBO cardTransactionBO;
	private RegistryBO registryBO;
	private EkeymanService ekeymanService;
	
	@Test
	public void testEnd2End() {
		
		System.out.println("testEnd2End entered...");
		
		Registry registry = getRegistryBO().getRegistry();
		
		if(registry != null && registry.getPublicKey() != null  && !registry.getPublicKey().equalsIgnoreCase("")){
			
			EncryptionKeys encryptionKeys = getEkeymanService().getEncryptionKeys(
					"safetyword", 
					registry.getPublicKey(), 
					registry.getPrivateKey(),
					"App Server");
		
			String clearSafetyword = "";
			
			try{
				clearSafetyword = SecureCreditUtils.decrypt(encryptionKeys, registry.getCipherSafetyword());
			}
			catch(Exception e){
				// Sometimes an exception can occur when trying to decrypt without the original keys!
				// This shouldn't occur in this scenario
				clearSafetyword = "***BOGUS***";
			}

		
			assert registry.getClearSafetyword().equalsIgnoreCase(clearSafetyword) :
				"registry.getClearSafetyword(): " + 
				registry.getClearSafetyword() +
				" does not equal clearSafetyword: " 
				+ clearSafetyword + " ***This is normal if new private key was generated***";
		
		
			CreditCard newCreditCard = new CreditCard();
			newCreditCard.setCreditCardNumber("0000000000003456");
			newCreditCard.setCardType("Master Card");
			newCreditCard.setFourDigits("3456");
			newCreditCard.setExpirationDate("12/2014");
			newCreditCard.setNameOnCard("Jay Thomas");
			
			getCreditCardBO().saveCreditCard(newCreditCard);
			System.out.println("testEnd2End saveCreditCard...");		
			
			CreditCard existingCreditCard = getCreditCardBO().findCreditCard("3456", "Jay Thomas");
			System.out.println("testEnd2End findCreditCard (new card)...");
			
			
			assert existingCreditCard.getCreditCardNumber().equalsIgnoreCase("0000000000003456") :
				"existingCreditCard.getCreditCardNumber(): " + 
				existingCreditCard.getCreditCardNumber() +
				" does not equal newCreditCard.getCreditCardNumber(): " 
				+ "0000000000003456";
			
			
			
			CardTransaction cardTransaction1 = new CardTransaction();
			//cardTransaction1.setCreditCard(existingCreditCard);
			cardTransaction1.setTransactionNumber("171717171717171");
			cardTransaction1.setAmount(new BigDecimal("1000.11"));
			cardTransaction1.setPostDate("12/06/2010");
			cardTransaction1.setTransactionDate("12/04/2010");
			cardTransaction1.setVendorCode("WEGMANS 777556");
			//getCardTransactionBO().saveCardTransaction(cardTransaction1);
			getCardTransactionBO().saveCardTransaction(existingCreditCard.getCreditCardNumber(), cardTransaction1);
			
			
			CardTransaction cardTransaction2 = getCardTransactionBO().findCardTransaction("WEGMANS 777556");
			
			assert cardTransaction2.getTransactionNumber().equalsIgnoreCase("171717171717171") :
				"cardTransaction2.getTransactionNumber(): " + 
				cardTransaction2.getTransactionNumber() +
				" does not equal cardTransaction1.getTransactionNumber(): " 
				+ "171717171717171";
			
			getCardTransactionBO().deleteCardTransaction(cardTransaction2.getId());
			
			CardTransaction cardTransaction3 = getCardTransactionBO().findCardTransaction("WEGMANS 777556");
			
			assert cardTransaction3 == null : "Card Transaction not deleted";
			
			
			
			getCreditCardBO().deleteCreditCard(existingCreditCard.getId());
			System.out.println("testEnd2End deleteCreditCard...");
			
			CreditCard deletedCreditCard = getCreditCardBO().findCreditCard("3456", "Jay Thomas");
			System.out.println("testEnd2End findCreditCard (deleted card)...");
			
			assert deletedCreditCard == null : "Card not deleted";
		}
		else {
			System.out.println("testEnd2End...Regsitry inaccessible");
		}
		
		System.out.println("testEnd2End exited...");
	}
	
	
	@BeforeSuite
	public void beforeSuite() {
		System.out.println("Before suite...");
		
		ctx = new ClassPathXmlApplicationContext(SPRING_CONFIG_FILES);
		
		setRegistryBO((RegistryBO)ctx.getBeanFactory().getBean("registryBO"));
		setEkeymanService((EkeymanService)ctx.getBeanFactory().getBean("ekeymanService"));
		setCreditCardBO((CreditCardBO)ctx.getBeanFactory().getBean("creditCardBO"));
		setCardTransactionBO((CardTransactionBO)ctx.getBeanFactory().getBean("cardTransactionBO"));
	}
	
	@AfterSuite
	public void afterSuite() {
		System.out.println("After suite...");
	}


	public void setCreditCardBO(CreditCardBO creditCardBO) {
		this.creditCardBO = creditCardBO;
	}


	public CreditCardBO getCreditCardBO() {
		return creditCardBO;
	}


	public void setCardTransactionBO(CardTransactionBO cardTransactionBO) {
		this.cardTransactionBO = cardTransactionBO;
	}


	public CardTransactionBO getCardTransactionBO() {
		return cardTransactionBO;
	}


	public void setRegistryBO(RegistryBO registryBO) {
		this.registryBO = registryBO;
	}


	public RegistryBO getRegistryBO() {
		return registryBO;
	}


	public void setEkeymanService(EkeymanService ekeymanService) {
		this.ekeymanService = ekeymanService;
	}


	public EkeymanService getEkeymanService() {
		return ekeymanService;
	}

}
